﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using DTO;

namespace DAO
{
    public class ChiTietPhieuNhapDAO
    {
        DataProvider db;
        SachDAO sdao = new SachDAO();
        public ChiTietPhieuNhapDAO()
        {
            db = new DataProvider();
        }
        public DataTable LayDSChiTietPhieuNhap(int maphieunhap)
        {
            DataTable table = new DataTable();
            string sql = "select s.tensach,tl.tentheloai, s.tacgia, ctpn.soluongnhap, ctpn.machitietphieunhap, ctpn.masach from ChiTietPhieuNhap ctpn, Sach s, TheLoai tl where ctpn.maphieunhap = " + maphieunhap + " and ctpn.masach = s.masach and s.matheloai = tl.matheloai";
            table = db.LayBangDuLieu(sql);
            return table;
        }
        public ChiTietPhieuNhapDTO LayMotChiTietPhieuNhapSach(int machitietphieunhap)
        {
            DataTable table = new DataTable();
            string sql = "select * from ChiTietPhieuNhap where machitietphieunhap = " + machitietphieunhap + "";
            table = db.LayBangDuLieu(sql);
            ChiTietPhieuNhapDTO ctpndto = new ChiTietPhieuNhapDTO();
            DataRow dtr = table.Rows[0];

            ctpndto.Machitietphieunhap = int.Parse(dtr[0].ToString());
            ctpndto.Maphieunhap = int.Parse(dtr[1].ToString());
            ctpndto.Masach = int.Parse(dtr[2].ToString());
            ctpndto.Soluongnhap = int.Parse(dtr[3].ToString());

            return ctpndto;
        }
        public bool CapNhatChiTietPhieuNhapSach(ChiTietPhieuNhapDTO ctpndto)
        {
            bool flag = true;
            string sql = "update ChiTietPhieuNhap set masach = " + ctpndto.Masach + ", soluongnhap = " + ctpndto.Soluongnhap + " where machitietphieunhap = " + ctpndto.Machitietphieunhap + "";
            if (!db.ThucHienCauTruyVan(sql)) flag = false;
            return flag;
        }
        public bool XoaChiTietPhieuNhapSach(int machitietphieunhap)
        {
            bool flag = true;
            ChiTietPhieuNhapDTO ctpndto = LayMotChiTietPhieuNhapSach(machitietphieunhap);
            int masach = ctpndto.Masach;
            int soluongnhap = ctpndto.Soluongnhap;
            SachDTO sdto = sdao.LayMotCuonSach(masach);
            int soluongton = sdto.Soluongton;
            int soluong = soluongton - soluongnhap;
            string sql1 = "update Sach set soluongton = " + soluong + " where masach = " + masach + "";
            if (!db.ThucHienCauTruyVan(sql1)) flag = false;
            string sql = "delete from ChiTietPhieuNhap where machitietphieunhap = " + machitietphieunhap + "";
            if (!db.ThucHienCauTruyVan(sql)) flag = false;
            return flag;
        }
        public bool ThemChiTietPhieuNhapSach(ChiTietPhieuNhapDTO ctpndto)
        {
            bool flag = true;
            int masach = ctpndto.Masach;
            int soluongnhap = ctpndto.Soluongnhap;
            SachDTO sdto = sdao.LayMotCuonSach(masach);
            int soluongton = sdto.Soluongton;
            int soluong = soluongton + soluongnhap;
            string sql1 = "update Sach set soluongton = " + soluong + " where masach = " + masach + "";
            if (!db.ThucHienCauTruyVan(sql1)) flag = false;
            string sql = "insert into ChiTietPhieuNhap(maphieunhap, masach, soluongnhap) values (" + ctpndto.Maphieunhap + ", " + ctpndto.Masach + ", " + ctpndto.Soluongnhap + ")";
            if (!db.ThucHienCauTruyVan(sql)) flag = false;
            return flag;
        }
        public bool KiemTraSachTrongPhieuNhap(int masach)
        {
            bool flag = false;
            string sql = "select * from ChiTietPhieuNhap where masach = " + masach + "";
            DataTable table = new DataTable();
            table = db.LayBangDuLieu(sql);
            if (table.Rows.Count > 0)
                flag = true;
            return flag;
        }
    }
}
